<?php
include_once "../init.inc.php";
include_once '../inc/header.php';

$sql = "SELECT c.ID, c.cat_name, count(p.ID) as count 
                        FROM `posts` as p 
                        LEFT JOIN `relations` AS r ON p.ID = r.post_id
                        LEFT JOIN `category` AS c ON r.cat_id = c.ID
                        GROUP BY c.cat_name";

$result = mysql_query($sql) or die('Error, insert query failed');







?>
<h2 class="text-info">Niche Viewer</h2>
<strong><p>
Perhaps, you don't know exactly what are you looking for and just want to find all products in a specific niche.<br>
Then this section is for you. Choose your niche below:</p></strong>

<ul>    
                <?php    while ($row = mysql_fetch_assoc($result)) { ?>
                               
    <li>         
                    <?php    foreach ($row as $key=>$cellValue){ 
                                       if ($key=="ID") continue;
                                        if ($key=="cat_name")  {
                                                if ($cellValue==""){
                                                    //пустое имя категории
                                                    ?>
                                                
                                                     <a class ="lead" href="<?php echo "niche_view.php?cat=".$row['ID']."&cat_name=noname";?>"> 
                                                            <?php echo "noname"; ?>
                                                     </a>
                                               
                                                    <?php }else{
                                                        //непустая категория
                                                        ?>
                                             
                                                        <a class ="lead" href="<?php echo "niche_view.php?cat=".$row['ID']."&cat_name=".$row['cat_name'];?>"> 
                                                            <?php echo $cellValue; ?>
                                                        </a>
                                              
                                                        <?php } ?>
                                         <?php }else {
                                             // остальные не cat_name
                                             ?>
                                          
                                           
                                                <strong><span class="text-info"> <?php echo "(".$cellValue; ?></span>&nbsp;items<span class="text-info">)</span></strong><br>
                                          
                                          
                                             
                                       <?php       
                                         }
                                                             
                                                                } ?>
                                        
       </li>               

             <?php      }   ?>

</ul>
<?php  include_once 'footer.php'; ?>